package com.easylive.admin.controller;

import com.easylive.entity.component.RedisComponent;
import com.easylive.entity.config.AppConfig;
import com.easylive.entity.dto.TokenUserInfoDto;
import com.easylive.entity.request.account.AdminLoginReqVO;
import com.easylive.entity.request.account.WebLoginReqVO;
import com.easylive.entity.response.account.CheckCodeRspVO;
import com.easylive.entity.vo.ResponseVO;
import com.easylive.exception.BusinessException;
import com.easylive.service.UserInfoService;
import com.easylive.utils.StringTools;
import com.wf.captcha.GifCaptcha;
import com.wf.captcha.base.Captcha;
import lombok.extern.slf4j.Slf4j;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import java.awt.*;
import java.io.IOException;
import java.util.Objects;

/**
 * 用户信息表 Controller
 */
@RestController("accountController")
@RequestMapping("/account")
@Slf4j
public class AccountController extends ABaseController {

	@Resource
	private UserInfoService userInfoService;

	@Resource
	private RedisComponent redisComponent;

	@Resource
	private AppConfig appConfig;

	@PostMapping("/checkCode")
	public ResponseVO checkCode() throws IOException, FontFormatException {
		GifCaptcha gifCaptcha = new GifCaptcha(100, 42);
		gifCaptcha.setFont(Captcha.FONT_1);

		String code = gifCaptcha.text();
		log.info("登录验证码：{}", code);

		String captchaBase64 = gifCaptcha.toBase64();
		String checkCodeKey = redisComponent.saveCheckCode(code);

		CheckCodeRspVO checkCodeRspVO = new CheckCodeRspVO();
		checkCodeRspVO.setCheckCodeKey(checkCodeKey);
		checkCodeRspVO.setCheckCode(captchaBase64);
		return getSuccessResponseVO(checkCodeRspVO);
	}

	@PostMapping("/login")
	public ResponseVO login(@Validated AdminLoginReqVO reqVO, HttpServletResponse response) {

		try {
			// 校验验证码
			validateCheckCode(reqVO.getCheckCodeKey(), reqVO.getCheckCode());

			if (!(Objects.equals(appConfig.getAdminAccount(), reqVO.getAccount()) &&
					Objects.equals(StringTools.encodePassword(appConfig.getAdminPassword()), reqVO.getPassword()))) {
				throw new BusinessException("账号或密码错误");
			}

			String token = redisComponent.saveTokenInfo4Admin(reqVO.getAccount());
			saveToken2Cookie(response, token);

			return getSuccessResponseVO(null);
		} finally {
			redisComponent.deleteCheckCodeByKey(reqVO.getCheckCodeKey());
		}
	}

	@PostMapping("/logout")
	public ResponseVO logout(HttpServletResponse response) {
		cleanToken(response);
		return getSuccessResponseVO(null);
	}


	private void validateCheckCode(String checkCodeKey, String checkCode) {
		String redisCheckCode = redisComponent.getCheckCodeByKey(checkCodeKey);
		if (StringTools.isEmpty(redisCheckCode)) {
			throw new BusinessException("验证码已过期，请重新获取");
		}

		if (!redisCheckCode.equalsIgnoreCase(checkCode)) {
			throw new BusinessException("验证码错误");
		}
	}

}